package system

import (
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	s1 "MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
)

type RoleAuthUserCtrl struct {
	base.BaseCtrl
}

func (s *RoleAuthUserCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleRoleAuthUserMsg)
	return msg
}

// 查询已分配用户角色列表
// @Tags 角色管理
// @Summary 查询已分配用户角色列表
// @Security ApiKeyAuth
// @Param data query s1.RoleAllocatedUserItemReq true "查询已分配用户角色列表"
// @success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "ok"}"
// @Router /system/role/authUser/allocatedList [get]
func (s *RoleAuthUserCtrl) GetAllocatedListPage(c *gin.Context) {
	userItemReq := &s1.RoleAllocatedUserItemReq{}
	if err := c.ShouldBindQuery(userItemReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	userItemReq.ShouldBindParams(c)
	if pageResult, err := services.ServiceGroupInst.SystemGroupInst.RoleAuthUserServiceInst.GetAllocatedListPage(c, userItemReq); err != nil {
		global.MoLogger.Error("获取已分配用户列表失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_ErrorGetAllocatedUserListFail)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
	}

}

// 查询未分配用户角色列表
// @Tags 角色管理
// @Summary 查询未分配用户角色列表
// @Security ApiKeyAuth
// @Param data query s1.RoleAllocatedUserItemReq true "查询未分配用户角色列表"
// @success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "ok"}"
// @Router /system/role/authUser/unallocatedList [get]
func (s *RoleAuthUserCtrl) GetUnallocatedListPage(c *gin.Context) {
	userItemReq := &s1.RoleAllocatedUserItemReq{}
	if err := c.ShouldBindQuery(userItemReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	userItemReq.ShouldBindParams(c)
	if pageResult, err := services.ServiceGroupInst.SystemGroupInst.RoleAuthUserServiceInst.GetUnallocatedListPage(c, userItemReq); err != nil {
		global.MoLogger.Error("获取已分配用户列表失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_ErrorGetAllocatedUserListFail)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
	}
}

// 批量选择用户授权
// @Tags 角色管理
// @Summary 批量选择用户授权
// @Security ApiKeyAuth
// @Param data body s1.SysRoleAuthUsersReq true "批量选择用户授权"
// @success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "ok"}"
// @Router /system/role/authUser/selectAll [PUT]
func (s *RoleAuthUserCtrl) AuthUserAll(c *gin.Context) {
	authUserAllReq := &s1.SysRoleAuthUsersReq{}
	if err := c.ShouldBindQuery(authUserAllReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if len(*authUserAllReq.UserIds) > 0 {
		userIds := strings.Split((*authUserAllReq.UserIds)[0], ",")
		authUserAllReq.UserIds = &userIds
	}
	if _, err := services.ServiceGroupInst.SystemGroupInst.RoleAuthUserServiceInst.AuthUserAll(c, authUserAllReq); err != nil {
		global.MoLogger.Error("批量授权用户失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_AuthUserAllFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_AuthUserAllSuccessMsg, nil)
}

// 取消授权用户
// @Tags 角色管理
// @Summary 取消授权用户
// @Security ApiKeyAuth
// @Param data body s1.SysRoleAuthUserReq true "取消授权用户"
// @success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "ok"}"
// @Router /system/role/authUser/cancel [put]
func (s *RoleAuthUserCtrl) CancelAuthUser(c *gin.Context) {
	roleAuthUser := s1.SysRoleAuthUserReq{}
	if err := c.ShouldBindJSON(&roleAuthUser); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if _, err := services.ServiceGroupInst.SystemGroupInst.RoleAuthUserServiceInst.CancelAuthUser(&roleAuthUser); err != nil {
		global.MoLogger.Error("取消授权失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_AuthUserCancelAuthFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_AuthUserCancelAuthSuccessMsg, nil)
}

// 批量取消授权
// @Tags 角色管理
// @Summary 批量取消授权
// @Security ApiKeyAuth
// @Param data body s1.SysRoleAuthUsersReq true "批量取消授权"
// @success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "ok"}"
// @Router /system/role/authUser/cancelAll [put]
func (s *RoleAuthUserCtrl) CancelAuthUserAll(c *gin.Context) {
	roleAuthUsers := s1.SysRoleAuthUsersReq{}
	if err := c.ShouldBindJSON(&roleAuthUsers); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if _, err := services.ServiceGroupInst.SystemGroupInst.RoleAuthUserServiceInst.CancelAuthUsers(&roleAuthUsers); err != nil {
		global.MoLogger.Error("批量取消授权失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_AuthUserCancelAllFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_AuthUserCancelAllSuccessMsg, nil)
}
